package com.pan.utils;

import java.io.*;
import java.util.Properties;

public enum ConfigUtils{
    INSTANCE;


    private Properties configProperties;

    ConfigUtils(){
        configProperties = new Properties();
        InputStream configInputStream = ConfigUtils.class
                .getClassLoader().getResourceAsStream("HumbleServerConfig.properties");
        Reader configReader = new InputStreamReader(configInputStream);
        try {
            configProperties.load(configReader);
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }

    public String getStringValue(String key){
        String value = configProperties.getProperty(key);
        if (value == null){
            throw new RuntimeException(key + " configuration error");
        }
        return value.trim();
    }

    public Integer getIntValue(String key){
        String value = getStringValue(key);
        if (value.matches("^[-\\+]?\\d+$")){
            return Integer.parseInt(value);
        }
        throw new RuntimeException(key + " is not a Integer");
    }

    public Double getDoubleValue(String key){
        String value = getStringValue(key);
        if (value.matches("^[-\\+]?([1-9]\\d+|0)(\\.\\d+)?$")){
            return Double.parseDouble(value);
        }
        throw new RuntimeException(key + " is not a Double");
    }


}
